<template>
	<view class="comment">
		<u-sticky bgColor="#fff">
			<u-tabs :list="tabs" @click="handleChangeTab"></u-tabs>
		</u-sticky>
		<view v-if="list.length">
			<CommentBox :commentList="list" ></CommentBox>
			<u-loadmore :status="loadmoreStatus" line />
		</view>
		<view class="empty" v-else>
			<u-empty mode="search" :iconSize="120" text="数据为空"></u-empty>
		</view>
		<view class="footer">
			<view class="btn" @click="toOrder">
				去评价
			</view>
		</view>
	</view>
</template>

<script>
	import CommentBox from './components/comment-box.vue';

	export default {
		components: { CommentBox },
		data() {
			return {
				page: 1,
				size: 10,
				tabs: [{
						name: '精选',
						value: 'FEATURED'
					},
					{
						name: '普通',
						value: 'NOT_PUBLISHED'
					},
					{
						name: '待审核',
						value: 'NOT_FEATURED'
					},
				],
				status: 'FEATURED',
				list: [],
				loadmoreStatus: 'loadmore',
			}
		},
		onLoad() {
			this.getList();
		},
		onPullDownRefresh() {
			this.page = 1;
			this.list = [];
			this.getList();
			uni.stopPullDownRefresh();
		},
		onReachBottom() {
			if(this.loadmoreStatus === 'nomore') return;
			this.loadmoreStatus = 'loading';
			this.page += 1;
			this.getList();
		},
		methods: {
			handleChangeTab(e) {
				this.status = e.value;
				this.page = 1;
				this.list = [];
				this.loadmoreStatus = 'loadmore';
				this.getList();
			},
			async getList() {
				uni.showLoading({
					title: '加载中...',
				});
				const res = await this.$api('/study/mobile/personal/center/comment/page', 'get', {
					page: this.page,
					limit: this.size,
					status: this.status,
				})
				uni.hideLoading();
				
				this.loadmoreStatus = 'loadmore';
				this.list = this.list.concat(res.data.list);
				
				if (res.data.list.length < this.size) {
					this.loadmoreStatus = 'nomore';
					return;
				}
			},
			toOrder() {
				uni.navigateTo({
					url: '/pages/my/order?currentTab=2',
				});
			},
		}
	}
</script>

<style lang="scss" scoped>
	.comment {
		margin: 30rpx;
		padding-bottom: 150rpx;

		.footer {
			position: fixed;
			left: 0;
			bottom: 0;
			right: 0;

			.btn {
				width: 100%;
				height: 120rpx;
				line-height: 120rpx;
				background-color: #74BB2A;
				text-align: center;
				color: #fff;
			}
		}
	}
</style>